using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace Sustainalytics.Universes.Synchronization
{
    public abstract class AssureFeatureUniverseBy<T> : IAssureFeatureUniverse
    {
        protected IMongoCollection<T> MongoCollection { get; set; }

        protected AssureFeatureUniverseBy(string connectionString)
        {
            CollectionName = typeof(T).Name;
            var mongoClient = new MongoClient(connectionString);
            var database = mongoClient.GetDatabase(connectionString.Split('/').Last().Trim());

            MongoCollection = database.GetCollection<T>(CollectionName);
        }

        public async Task UpdateUniverseIdsAsync(Dictionary<Guid, List<int>> universeIds)
        {
            await ClearUniverseIdsAsync();
            await AddUniverseIdsAsync(universeIds);
        }

        public string CollectionName { get; private set; }

        protected abstract Task<UpdateResult> ClearUniverseIdsAsync();

        protected abstract Task AddUniverseIdsAsync(Dictionary<Guid, List<int>> companyUniverses);
    }
}